﻿using System;
using System.Collections.Generic;
using System.Text;
using yb.commerce.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace yb.commerce.DAL
{
    public class ModuleRoleDAO
    {
        public bool Add(ModuleRole moduleRole)
        {
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "insert into module_role(module_id,role_id) values (@module_id,@role_id)";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@module_id", DbType.Int32, moduleRole.ModuleId);
            database.AddInParameter(cmd, "@role_id", DbType.Int32, moduleRole.RoleId);
            int row = database.ExecuteNonQuery(cmd);
            return row > 0;
        }

        public bool Delete(int Id)
        {
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "delete from module_role where Id=@Id";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@Id", DbType.Int32, Id);
            int row = database.ExecuteNonQuery(cmd);
            return row > 0;
        }

        public bool Update(ModuleRole moduleRole)
        {
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "update module_role set module_id=@module_id,role_id=@role_id where Id=@Id";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@module_id", DbType.Int32, moduleRole.ModuleId);
            database.AddInParameter(cmd, "@role_id", DbType.Int32, moduleRole.RoleId);
            database.AddInParameter(cmd, "@Id", DbType.Int32, moduleRole.Id);
            int row = database.ExecuteNonQuery(cmd);
            return row > 0;
        }

        public ModuleRole GetEntity(int Id)
        {
            ModuleRole moduleRole = new ModuleRole();
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "select module_id,role_id from module_role where Id=@Id";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@Id", DbType.Int32, Id);
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                if (reader.Read())
                {
                    moduleRole.Id = Id;
                    moduleRole.ModuleId = int.Parse(reader["module_id"].ToString());
                    moduleRole.RoleId = int.Parse(reader["role_id"].ToString());
                }
            }
            return moduleRole;
        }

        public DataRecordTable GetList(string fieldList, string orderField, bool orderBy, int pageIndex, int pageSize, string where)
        {
            DataRecordTable table = new DataRecordTable();
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand cmd = database.GetStoredProcCommand("proc_getpagedata");
            database.AddInParameter(cmd, "@TableName", DbType.String, "module_role");
            database.AddInParameter(cmd, "@FieldList", DbType.String, fieldList);
            database.AddInParameter(cmd, "@PageSize", DbType.Int32, pageSize);
            database.AddInParameter(cmd, "@PageIndex", DbType.Int32, pageIndex);
            database.AddInParameter(cmd, "@OrderField", DbType.String, orderField);
            database.AddInParameter(cmd, "@OrderType", DbType.Boolean, orderBy);
            database.AddInParameter(cmd, "@Where", DbType.String, where);
            database.AddOutParameter(cmd, "@RecordCount", DbType.Int32, 4);
            database.AddOutParameter(cmd, "@PageCount", DbType.Int32, 4);

            DataSet ds = database.ExecuteDataSet(cmd);
            int recordCount = Convert.ToInt32(database.GetParameterValue(cmd, "@RecordCount"));
            int pageCount = Convert.ToInt32(database.GetParameterValue(cmd, "@PageCount"));
            table.Table = ds.Tables[0];
            table.PageSize = pageSize;
            table.PageIndex = pageIndex;
            table.PageCount = pageCount;
            table.RecordCount = recordCount;
            return table;
        }
        public bool IsHaveRecord(ModuleRole moduleRole)
        {
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "select count(Id) from module_role where module_id=@module_id and role_id=@role_id";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@module_id", DbType.Int32, moduleRole.ModuleId);
            database.AddInParameter(cmd, "@role_id", DbType.Int32, moduleRole.RoleId);
            object obj = database.ExecuteScalar(cmd);
            if (obj == null || obj.ToString().Length == 0)
                return false;
            return int.Parse(obj.ToString()) > 0;
        }
        public bool Delete(ModuleRole moduleRole)
        {
            Database database = DatabaseFactory.CreateDatabase();
            string sql = "delete from module_role where module_id=@module_id and role_id=@role_id";
            DbCommand cmd = database.GetSqlStringCommand(sql);
            database.AddInParameter(cmd, "@module_id", DbType.Int32, moduleRole.ModuleId);
            database.AddInParameter(cmd, "@role_id", DbType.Int32, moduleRole.RoleId);
            int row = database.ExecuteNonQuery(cmd);
            return row > 0;
        }
    }
}
